package com.mickey.service.Imp;

import com.mickey.dao.OrderDao;
import com.mickey.domain.MiaoshaOrder;
import com.mickey.domain.MiaoshaUser;
import com.mickey.domain.OrderInfo;
import com.mickey.enums.OrderEnum;
import com.mickey.redis.OrderKey;
import com.mickey.redis.RedisService;
import com.mickey.service.OrderService;
import com.mickey.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Desc:
 * @Author:czx
 * @CreateDate:2019/1/10 0010$ 16:02$
 */
@Service
public class OrderServiceImp implements OrderService {

    @Autowired
    OrderDao orderDao;
    @Autowired
    RedisService redisService;

    @Override
    public MiaoshaOrder getMiaoshaOrderByUserIdGoodsId(Long userId, long goodsId) {
        return redisService.get(OrderKey.getMiaoshaOrderByUidGid,"" + userId + "_" + goodsId,MiaoshaOrder.class);
    }

    @Override
    public OrderInfo createOrder(MiaoshaUser user, GoodsVO good) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(good.getId());
        orderInfo.setGoodsName(good.getGoodsName());
        orderInfo.setGoodsPrice(good.getMiaoshaPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(OrderEnum.NOT.getStatus());
        orderInfo.setUserId(user.getId());
        orderDao.insert(orderInfo);
        MiaoshaOrder miaoshaOrder = new MiaoshaOrder();
        miaoshaOrder.setGoodsId(good.getId());
        miaoshaOrder.setOrderId(orderInfo.getId());
        miaoshaOrder.setUserId(user.getId());
        orderDao.insertMiaoshaOrder(miaoshaOrder);
        //秒杀订单放入缓存
        redisService.set(OrderKey.getMiaoshaOrderByUidGid,"" + user.getId() + "_" + good.getId(),miaoshaOrder);
        return orderInfo;
    }

    @Override
    public OrderInfo getOrderById(long orderId) {
        return orderDao.getOrderById(orderId);
    }
}
